
package com.aris.voicechecking;

import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.voicecheck.adapter.HistoryLessonAdapter;
import com.voicecheck.model.HistoryModel;
import com.voicecheck.provider.DataProviderV2;

public class LessonResultActivity extends ActionBarActivity {

    private RelativeLayout adView;

    private DataProviderV2 dataProvider;

    private ListView listHistory;

    private HistoryLessonAdapter adapter;

    private List<HistoryModel> listHis;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lesson_result);

        initial();
        execution();
    }

    private void initial() {
        adView = (RelativeLayout)findViewById(R.id.adView);
        dataProvider = new DataProviderV2(LessonResultActivity.this);
        listHistory = (ListView)findViewById(R.id.listHistory);
    }

    private void execution() {

        adView.setBackgroundResource(R.drawable.banner);

        adView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent webviewIntent = new Intent(LessonResultActivity.this, WebviewActivity.class);
                startActivity(webviewIntent);
                overridePendingTransition(R.drawable.anim_slide_in_left,
                        R.drawable.anim_slide_out_left);

            }
        });

        listHis = dataProvider.retrieveHistoryLesson();

        adapter = new HistoryLessonAdapter(listHis);
        listHistory.setAdapter(adapter);
        listHistory.setOnItemClickListener(itemClickEvent);

        // configuration actionbar
        // getSupportActionBar().setTitle(getString(R.string.back));
        ActionBar actionBar = getSupportActionBar();
        ColorDrawable colorDrawable = new ColorDrawable(getResources().getColor(R.color.main_color));
        actionBar.setBackgroundDrawable(colorDrawable);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP);
        View actionbarcustomeview = getLayoutInflater().inflate(R.layout.actionbar_center_title,
                (ViewGroup)(getSupportActionBar().getCustomView()));

        TextView center_title = (TextView)actionbarcustomeview.findViewById(R.id.center_title);
        center_title.setText(getString(R.string.title_activity_lesson_result));
        center_title.setTextColor(Color.WHITE);

        actionBar.setCustomView(actionbarcustomeview, new ActionBar.LayoutParams(
                ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.MATCH_PARENT,
                Gravity.CENTER));

    }

    OnItemClickListener itemClickEvent = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            HistoryModel hmodel = listHis.get(position);

            Intent dayresultIntent = new Intent(LessonResultActivity.this, DayResultActivity.class);
            dayresultIntent.putExtra("date", hmodel.getDate());

            startActivity(dayresultIntent);

            overridePendingTransition(R.drawable.anim_slide_in_left, R.drawable.anim_slide_out_left);

        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.lesson_result, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        overridePendingTransition(R.drawable.anim_slide_in_right, R.drawable.anim_slide_out_right);
    }
}
